package com.skyworth.web.admin.company.service.impl;

import com.skyworth.web.admin.company.domain.CompanyModel;
import com.skyworth.web.admin.company.form.CompanyForm;
import com.skyworth.web.admin.company.repository.CompanyRepository;
import com.skyworth.web.admin.company.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by skyworth on 2016/11/10.
 */
@Service
public class CompanyServiceImpl implements CompanyService{

	@Autowired
	private CompanyRepository companyRepository;

	@Override
	public CompanyModel addCompany(CompanyForm companyForm) {
		CompanyModel companyModel = new CompanyModel();
		companyModel.name = companyForm.getName();
		companyModel.appid = companyForm.getAppid();
		companyModel.appsecret = companyForm.getAppsecret();
		companyModel.ownerId = companyForm.getOwnerId();
		companyRepository.save(companyModel);
		return companyModel;
	}

	@Override
	public CompanyModel getCompany(long companyId) {
		return companyRepository.findOne(companyId);
	}

	@Override
	public boolean existByUser(long userId) {
		return companyRepository.exists(userId);
	}

	@Override
	public CompanyModel updateCompany(CompanyForm companyForm) {
		CompanyModel companyModel = companyRepository.findOne(companyForm.id);
		if(companyForm.getName()!= null){
			companyModel.setName(companyForm.getName());
		}
		if(companyForm.getAppid() != null){
			companyModel.setAppid(companyForm.getAppid());
		}

		if(companyForm.getAppsecret() != null){
			companyModel.setAppsecret(companyForm.getAppsecret());
		}
		companyRepository.save(companyModel);

		return companyModel;
	}
}
